/*
 * Copyright:  2018 smarabbit studio.
 *
 *  Licensed under the Confluent Community License; you may not use this file
 *  except in compliance with the License.  You may obtain a copy of the License at
 *
 *  http://www.confluent.io/confluent-community-license
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 *  WARRANTIES OF ANY KIND, either express or implied.  See the License for the
 *  specific language governing permissions and limitations under the License.
 *
 *  @作   者： 黄开晖 (117227773@qq.com)
 *  @日   期:  2021年5月28日
 */
package com.massyframework.beanskin.runtime.modular.assembly;

import java.util.Objects;

import com.massyframework.beanskin.assembly.Assembly;
import com.massyframework.beanskin.assembly.AssemblyConfig;
import com.massyframework.beanskin.assembly.AssemblyContext;
import com.massyframework.beanskin.assembly.AssemblyStatus;
import com.massyframework.beanskin.assembly.DependencyMatcher;
import com.massyframework.beanskin.assembly.beans.BeanContextHandlerFactory;
import com.massyframework.beanskin.assembly.handling.LifecycleManager;
import com.massyframework.beanskin.runtime.modular.AbstractModule;
import com.massyframework.beanskin.runtime.modular.AbstractModuleBuilder;
import com.massyframework.beanskin.runtime.modular.assembly.handling.LifecycleManagerFactory;

/**
 * 抽象的装配件
 * @author huangkh
 *
 */
abstract class AbstractAssembly extends AbstractModule<AssemblyConfig>
	implements Assembly{
	
	private LifecycleManager lifecycleManager;
	
	/**
	 * 构造方法
	 * @param builder
	 */
	public AbstractAssembly(DefaultAssemblyBuilder builder) {
		super(builder);		
		boolean hasDependencies = builder.dependencies != null ? 
				(builder.dependencies.isEmpty() ? false: true)
				: false;
		boolean hasExports = builder.exports != null ? 
				(builder.exports.isEmpty() ? false: true)
				: false;
		
		BeanContextHandlerFactory factory = Objects.requireNonNull(
				builder.beanContextHandlerFactory, "\"beanContextHandlerFactory\" cannot be null.");
		
		this.lifecycleManager = LifecycleManagerFactory.createLifecycleManager(
				this, null, builder.beanMap, factory, hasDependencies, hasExports);
		try {
			this.lifecycleManager.register(this.getModuleRepository());
		} catch (Exception e) {
		}
	}
	

	@Override
	public AssemblyContext getAssemblyContext() {
		return this.lifecycleManager.findHandler(AssemblyContext.class);
	}

	@Override
	public AssemblyStatus getAssemblyStatus() {
		return this.lifecycleManager.getAssemblyStatus();
	}
	
	
	
	
	@Override
	public <A> A adapt(Class<A> adaptType) {
		if (adaptType == DependencyMatcher.class) {
			return this.getLifecycleManager().findHandler(adaptType);
		}
		return super.adapt(adaptType);
	}


	@Override
	protected AssemblyConfig createConfig(AbstractModuleBuilder builder) {
		return new DefaultAssemblyConfig((DefaultAssemblyBuilder)builder);
	}
		
	/**
	 * 生命周期管理器
	 * @return
	 */
	protected LifecycleManager getLifecycleManager() {
		return this.lifecycleManager;
	}
}
